﻿#include "mongoagent.h"
#include <log/log.h>

bool MongoAgent::getConnectStatus()
{  
    try
    {
        mongoDBer.getClient()["admin"].list_collection_names();        
        return true;
    }
    catch(std::exception& e)
    {
        //HIM_ERROR("MongoDB error: {}", e.what());
        return false;
    }    
}

long MongoAgent::count(std::string dbName,std::string collectionName,bsoncxx::document::view doc)
{
    if(getConnectStatus())
        return mongoDBer.getClient()[dbName][collectionName].count_documents(doc);
    else
        return -1;
}

void MongoAgent::insert(std::string dbName,std::string collectionName,bsoncxx::document::view doc)
{
    if(getConnectStatus())
        mongoDBer.getClient()[dbName][collectionName].insert_one(doc);
}

void MongoAgent::update(std::string dbName, std::string collectionName,
                       bsoncxx::document::view filter,bsoncxx::document::view doc)
{
    if(getConnectStatus())
        mongoDBer.getClient()[dbName][collectionName].find_one_and_update(filter, doc);
}

void MongoAgent::updateOne(std::string dbName, std::string collectionName,
                       bsoncxx::document::view filter,bsoncxx::document::view doc)
{
    if(getConnectStatus())
        mongoDBer.getClient()[dbName][collectionName].update_one(filter, doc);
}

void MongoAgent::remove(std::string dbName,std::string collectionName,bsoncxx::document::view doc)
{
    if(getConnectStatus())
         mongoDBer.getClient()[dbName][collectionName].delete_one(doc);
}

